package com.jwthhealth_pub.old.report.view.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.report.model.resbody.PhysDescRes;
import com.jwthhealth_pub.old.report.view.IndicalPhysicalActivity;

import java.util.List;

/**
 * Created by liuzhen on 2017/10/26.
 */

public class PhysDescAdapter extends RecyclerView.Adapter {
    private final IndicalPhysicalActivity mActivity;
    private final List<PhysDescRes.DataBean.SonBeanX> mData;

    public PhysDescAdapter(IndicalPhysicalActivity indicalPhysicalActivity, List<PhysDescRes.DataBean.SonBeanX> data) {
        this.mActivity = indicalPhysicalActivity;
        this.mData = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(mActivity, R.layout.item_key_list_report, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        PhysDescRes.DataBean.SonBeanX sonBeanX = mData.get(vh.getAdapterPosition());

        vh.key.setText(sonBeanX.getBname());
        final List<PhysDescRes.DataBean.SonBeanX.SonBean> son = sonBeanX.getSon();

        if (son == null || son.size() <= 0) return;
        vh.valueList.setLayoutManager(new LinearLayoutManager(mActivity));
        vh.valueList.setAdapter(new InnerAdapter(son));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        private TextView key;
        private RecyclerView valueList;

        public ViewHolder(View itemView) {
            super(itemView);
            key = itemView.findViewById(R.id.tv_key);
            valueList = itemView.findViewById(R.id.rv_list);
        }
    }

    private class InnerAdapter extends RecyclerView.Adapter{

        List<PhysDescRes.DataBean.SonBeanX.SonBean> son;

        InnerAdapter(List<PhysDescRes.DataBean.SonBeanX.SonBean> son) {
            this.son = son;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new InnerVh(View.inflate(mActivity, R.layout.item_key_value_report, null));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            InnerVh ivh = (InnerVh) holder;

            PhysDescRes.DataBean.SonBeanX.SonBean sonBean = son.get(ivh.getAdapterPosition());
            ivh.key.setText(sonBean.getBvalue());
            ivh.value.setText(sonBean.getBchar());
        }

        @Override
        public int getItemCount() {
            return son.size();
        }

        class InnerVh extends RecyclerView.ViewHolder {
            private TextView key, value;

            InnerVh(View itemView) {
                super(itemView);
                key = itemView.findViewById(R.id.tv_key);
                value = itemView.findViewById(R.id.tv_value);
            }
        }
    }
}
